package com.npy.reggie.config;

import com.npy.reggie.doman.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

@ControllerAdvice(annotations = {Controller.class, RestController.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> reusername(SQLIntegrityConstraintViolationException sqlex) {
        String localizedMessage = sqlex.getLocalizedMessage();
        log.info("{}", localizedMessage);


        if (localizedMessage.contains("Duplicate entry")) {
            return R.error(localizedMessage.split(" ")[2]);
        }

        return R.error(sqlex.getLocalizedMessage());

    }

    @ExceptionHandler(CustomException.class)
    public R<String> reFoot(CustomException ceex) {


        return R.error(ceex.getMessage());
    }
}
